package com.isoft.airportsystem.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isoft.airportsystem.entity.system.Permission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 权限Mapper接口
 */
@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 根据角色ID查询权限列表
     *
     * @param roleId 角色ID
     * @return 权限列表
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON p.permission_id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId} AND p.status = 1 AND p.del_flag = 0 " +
            "ORDER BY p.sort")
    List<Permission> selectPermissionsByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据用户ID查询权限列表
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("SELECT DISTINCT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON p.permission_id = rp.permission_id " +
            "LEFT JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1 AND p.del_flag = 0 " +
            "ORDER BY p.sort")
    List<Permission> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 查询菜单权限列表
     *
     * @return 菜单权限列表
     */
    @Select("SELECT * FROM sys_permission WHERE permission_type = 1 AND status = 1 AND del_flag = 0 ORDER BY sort")
    List<Permission> selectMenuList();

    /**
     * 根据用户ID查询菜单权限列表
     *
     * @param userId 用户ID
     * @return 菜单权限列表
     */
    @Select("SELECT DISTINCT p.* FROM sys_permission p " +
            "LEFT JOIN sys_role_permission rp ON p.permission_id = rp.permission_id " +
            "LEFT JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.permission_type = 1 AND p.status = 1 AND p.del_flag = 0 " +
            "ORDER BY p.sort")
    List<Permission> selectMenuListByUserId(@Param("userId") Long userId);
}